/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-04-20 12:33:49
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-06-08 16:28:13
 * @FilePath: \label_rack_control\firmware\bsp\bsp_timer.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "bsp_timer.h"

static void bsp_timer1_init(uint8_t repetition, uint32_t cycle);
static void bsp_timer3_init(uint32_t num);

/*
* Function Name  : bsp_timer4_init
* Description    : 1ms时基中断 驱动初始化
* Input          : None
* Output         : None
* Return         : None
*/
void bsp_timer4_init(void) 
{
#ifdef MCU_TYPE_APM

    TMR_BaseConfig_T TMR_BaseConfigStruct;
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR4);

    TMR_BaseConfigStruct.clockDivision = TMR_CLOCK_DIV_1;
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP;
    TMR_BaseConfigStruct.division = 71;
    TMR_BaseConfigStruct.period = 999;
    TMR_BaseConfigStruct.repetitionCounter = 0;
    TMR_ConfigTimeBase(TMR4, &TMR_BaseConfigStruct);

    TMR_EnableInterrupt(TMR4, TMR_INT_UPDATE);
    NVIC_EnableIRQRequest(TMR4_IRQn, 0, 0);

    TMR_Enable(TMR4);
#else
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    // 使能TIM1时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    // 时钟分频
    TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1); // 64 - 1 // 1us计数一次
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
    TIM_TimeBaseStructure.TIM_Period = 10; // 计数器周期为10000-1 (10ms中断)
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
    // 使能更新中断
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
    // 中断优先级设置
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    // 使能TIM1
    TIM_Cmd(TIM1, ENABLE);
#endif
}

/*
* Function Name  : bsp_timer4_init
* Description    : 输入捕获
* Input          : None
* Output         : None
* Return         : None
*/
void bsp_timer2_init(void)
{
    GPIO_Config_T gpioConfig;
    TMR_ICConfig_T ICConfig;

    /* Enable Periph Clock */
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR2);
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);

    /* TMR2 channel1 for PA0 */
    gpioConfig.pin = GPIO_PIN_0;
    gpioConfig.mode = GPIO_MODE_IN_PU;
    gpioConfig.speed = GPIO_SPEED_50MHz;
    GPIO_Config(GPIOA, &gpioConfig);

    /* TMR2 PWM Input mode */
    ICConfig.channel = TMR_CHANNEL_1;
    ICConfig.polarity = TMR_IC_POLARITY_FALLING; 
    ICConfig.selection = TMR_IC_SELECTION_DIRECT_TI;
    ICConfig.prescaler = TMR_IC_PSC_1;
    ICConfig.filter = 0x00;
    TMR_ConfigPWM(TMR2, &ICConfig);

    /* Enable TMR4 Interrupt */
    TMR_Enable(TMR2);
    NVIC_EnableIRQRequest(TMR2_IRQn, 0, 0);
    TMR_EnableInterrupt(TMR2, TMR_INT_CC1);
}

/*
* Function Name  : bsp_timer23_init
* Description    : 主从配置,输出固定数量脉冲 用timer2 去触发timer3 timer2的ch发出PWM波形
* Input          : None
* Output         : None
* Return         : None
*/
void bsp_timer31_init(void)
{
    //bsp_timer1_init(10, 20);
    //bsp_timer3_init(1000);
}

/*
* Function Name  : bsp_timer2_init
* Description    : 主定时器配置
* Input          : None
* Output         : None
* Return         : None
*/
static void bsp_timer3_init(uint32_t cycle)
{
    GPIO_Config_T gpioConfig;
    TMR_OCConfig_T OCcongigStruct;
    TMR_BaseConfig_T TMR_BaseConfigStruct;

    /* Enable Periph Clock */
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);

    GPIO_ConfigPinRemap(GPIO_FULL_REMAP_TMR3);

    /* TMR3 channel4 for PC9 */
    gpioConfig.pin = GPIO_PIN_9;
    gpioConfig.mode = GPIO_MODE_AF_PP;
    gpioConfig.speed = GPIO_SPEED_50MHz;
    GPIO_Config(GPIOC, &gpioConfig);

    TMR_BaseConfigStruct.clockDivision = TMR_CLOCK_DIV_1;
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP;
    TMR_BaseConfigStruct.division = (72 - 1); 
    TMR_BaseConfigStruct.period = (cycle - 1);
    TMR_BaseConfigStruct.repetitionCounter = 0;
    TMR_ConfigTimeBase(TMR3, &TMR_BaseConfigStruct);

    TMR_ConfigOCStructInit(&OCcongigStruct);
    OCcongigStruct.idleState = TMR_OC_IDLE_STATE_RESET;
    OCcongigStruct.mode = TMR_OC_MODE_PWM1;
    OCcongigStruct.nIdleState = TMR_OC_NIDLE_STATE_RESET;
    OCcongigStruct.nPolarity = TMR_OC_NPOLARITY_HIGH;
    OCcongigStruct.outputNState = TMR_OC_NSTATE_ENABLE;
    OCcongigStruct.outputState = TMR_OC_STATE_ENABLE;
    OCcongigStruct.polarity = TMR_OC_POLARITY_HIGH;
    OCcongigStruct.pulse = cycle / 2;
    TMR_ConfigOC4(TMR3, &OCcongigStruct);
    TMR_ConfigOC4Preload(TMR3, TMR_OC_PRELOAD_ENABLE);

    TMR_SelectOutputTrigger(TMR3, TMR_TRGO_SOURCE_UPDATE);
    TMR_EnableMasterSlaveMode(TMR3);

    TMR_Enable(TMR3);
}

/**
 * uint32_t num : 脉冲数量
*/
static void bsp_timer1_init(uint8_t repetition, uint32_t num)
{
    TMR_BaseConfig_T TMR_BaseConfigStruct;
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR1);

    TMR_BaseConfigStruct.clockDivision = TMR_CLOCK_DIV_1;
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP;
    TMR_BaseConfigStruct.division = 0; 
    TMR_BaseConfigStruct.period = num - 1;
    /* (repetitionCounter = 1；10个计数 出20个脉冲) (= 2， 10个计数 出 30个脉冲) */
    TMR_BaseConfigStruct.repetitionCounter = repetition;  
    TMR_ConfigTimeBase(TMR1, &TMR_BaseConfigStruct);

    TMR_SelectSlaveMode(TMR1, TMR_SLAVE_MODE_EXTERNAL1); /* 触发源作为时钟 */
    TMR_SelectInputTrigger(TMR1, TMR_TRIGGER_SOURCE_ITR2);

    TMR_ClearIntFlag(TMR1, TMR_INT_UPDATE);
    TMR_ClearStatusFlag(TMR1, TMR_FLAG_UPDATE);
    TMR_EnableInterrupt(TMR1, TMR_INT_UPDATE);
    NVIC_EnableIRQRequest(TMR1_UP_IRQn, 0, 0);
    
    TMR_Enable(TMR1);
}

/**
 * 固定脉冲数量输出
*/
void bsp_timer31_pulse_output(uint8_t repetition, uint32_t cycle, uint32_t num)
{
    if(cycle == 0 || num == 0)
    {
        TMR_Disable(TMR1);
        TMR_Disable(TMR3);
    }
    else
    {
        TMR_Reset(TMR1);
        TMR_Reset(TMR3);
        bsp_timer3_init(cycle);
        bsp_timer1_init(repetition, num);
    }
}

/**
 * 重置count值
*/
void bsp_timer31_reset_count(void)
{
    TMR_ConfigCounter(TMR1, 0);
    TMR_ConfigCounter(TMR3, 0);
}

